﻿@model MvcApplication1.Models.ItemEdit

@{
    ViewBag.Title = "UserAddItem";
}

<h2>添加商品</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    var UploadifyAuthCookie = '@(Request.Cookies[FormsAuthentication.FormsCookieName] == null ? "" : Request.Cookies[FormsAuthentication.FormsCookieName].Value)';
    var UploadifySessionId = '@Session.SessionID';
    var oldimage = '';
    $(document).ready(
    function () {
        $('#fileuploader').uploadify({
            'swf': '/Scripts/uploadify.swf',
            //'swf': '/52l9/Scripts/uploadify.swf',
            'uploader': 'Upload',    //由于从usermanage到此页面的链接包含两个‘/’符合，从而会造成upload_url错误，从导致文件无法上传，报io error或http error
            'fileSizeLimit': '10MB',
            'debug': false,
            'fileTypeDesc': 'Image file',
            'fileTypeExts': '*.jpg;*.jpeg;*.gif;*.png',
            'buttonText': '选择上传图片',
            'multi': false,
            'onUploadStart': function (file) {
                $('#selected_file').text(file.name);
                //重复上传时，删除上一张图像
                $('#fileuploader').uploadify('settings', 'formData', { 'oldimage': oldimage, RequireUploadifySessionSync: true, SecurityToken: UploadifyAuthCookie, SessionId: UploadifySessionId });  //官网上关于settings的写法不行，要这样写才能更改formData
            },
            'onUploadSuccess': function (file, data, response) {
                var res = data.split('|');
                if (res[0] == 'ok') {
                    alert('图片上传成功');
                    $('#image').val(res[1]);
                    oldimage = res[1];
                    $('#itemimage').attr('src', '@Href("~/uploads/")' + res[1]);
                } else if (res[0] == 'Invaild file type') {
                    alert('图片格式不正确');
                } else {
                    alert('图片上传失败');
                }
            },
            'onUploadError': function (file, errorCode, errorMsg, errorString) {
                alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
            }
        });
        //oldimage = $('#image').val();     when page first loads, there is no upload image yet so oldimage is empty
        var image = document.getElementById('image');
        image.style.width = 0;
        image.style.height = 0;
        image.style.visibility = 'hidden';
    }

);
</script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ItemEdit</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.itemname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.itemname)
            @Html.ValidationMessageFor(model => model.itemname)
        </div>

        @*<div class="editor-label">
            @Html.LabelFor(model => model.itemSN)
        </div>*@
        <div class="editor-field">
            @*@Html.EditorFor(model => model.itemSN)
            @Html.ValidationMessageFor(model => model.itemSN)*@
            @Html.HiddenFor(Model => Model.itemSN)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.image)
        </div>
        <div class="editor-field">
            <label id="selected_file" >未上传图片</label><div id="fileuploader"></div>
            @*@Html.HiddenFor(model => model.image)*@
            @Html.EditorFor(model=>model.image)
            @Html.ValidationMessageFor(model => model.image)   
        </div>

        @*<div class="editor-label">
            @Html.LabelFor(model => model.addtime)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.addtime)
            @Html.ValidationMessageFor(model => model.addtime)
        </div>*@

        <div class="editor-label">
            @Html.LabelFor(model => model.linkAddr)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.linkAddr)
            @Html.ValidationMessageFor(model => model.linkAddr)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.price)
            @Html.ValidationMessageFor(model => model.price)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.intro)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.intro)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.keywordlist)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.keywordlist)
            @Html.ValidationMessageFor(model => model.keywordlist)<span>关键字之间用空格隔开</span>
        </div>
        @Html.Hidden("orginpagenumber", Request.QueryString["orginpagenumber"])
        @Html.Hidden("orginsortorder", Request.QueryString["orginsortorder"])
        @Html.Hidden("itemname_search", Request.QueryString["itemname_search"]) 
        @Html.Hidden("itemSN_search", Request.QueryString["itemSN_search"])
        @Html.Hidden("addtime_search", Request.QueryString["addtime_search"])
        @Html.Hidden("linkaddr_search", Request.QueryString["linkaddr_search"])
        <p>
            <input type="submit" value="添加" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("返回列表", "UserManage")
</div>
<div class="display-label">图片</div>
<div class="display-field"><img id="itemimage" alt="image" /></div>
